<template>
  <div class="hello" :class="{'loading':status=='1'}">
    <div v-html="htmls"></div>
    <van-swipe :autoplay="2000" :height="'auto'" indicator-color="#f1190c" class="banner">
      <van-swipe-item v-for="(image, index) in bannerList" :key="index" @click="go(item)">
        <img v-lazy="image.bannerUrl" />
      </van-swipe-item>
    </van-swipe>
    <van-form @failed="onFailed" @submit="onSubmit">
      <van-field
        v-model="account"
        name="账号"
        label="账  号"
        placeholder="请填入账号"
        :rules="[{ required: true,  message: '请填入账号'}]"
      />
      <van-field
        v-model="confirmAccount"
        name="确认账号"
        label="确认账号"
        placeholder="请填入确认账号"
        :rules="[{ required: true, message: '请填入确认账号' }]"
      />
      <van-field
        v-model="num"
        name="金额"
        label="金 额"
        placeholder="请输入整数金额，最低充值10元"
        style="margin-bottom:15px;"
        :rules="[{ pattern: patternNum, message: '请输入整数金额，最低充值10元' }]"
      />
      <van-field v-model="email" name="邮箱" label="邮箱" placeholder="请填入邮箱" />
      <van-cell-group>
        <van-cell
          v-for="item in payTypeLIst"
          :key="item.id"
          clickable
          @click="payType = item.payType"
        >
          <template #title>
            <img
              :src="item.payName == '支付宝' ? require('../imgs/icon_03.png') :  item.payName == '微信' ? require('../imgs/icon_14.png') : ''"
              class="img"
              width="27px"
              height="27px"
            />
            <span class="custom-title">{{item.payName}}</span>
            <img
              v-if="item.isRecommend == 0"
              :src="require('../imgs/icon_06.png')"
              class="img"
              alt
              width="38px"
              height="16px"
            />
          </template>
          <template #right-icon>
            <img
              v-if="payType == item.payType"
              width="20px"
              height="14px"
              :src="require('../imgs/icon_09.png')"
              class="img"
              alt
            />
          </template>
        </van-cell>
      </van-cell-group>
      <div style="margin: 45px 16px 0;">
        <van-button block type="info" native-type="submit">提交</van-button>
      </div>
    </van-form>
    <van-overlay :show="show">
      <div class="wrapper" @click.stop>
        <div class="block">
          <van-loading type="spinner" color="#f1190c">请稍后。。。</van-loading>
        </div>
      </div>
    </van-overlay>
    <van-dialog
      v-model="showDialog"
      title="请完成验证操作"
      confirmButtonText="刷新 "
      confirmButtonColor="#ee0a24"
      :closeOnClickOverlay="true"
      :beforeClose="beforeClose"
    >
      <div class="contents">
        <div class="imgs" v-if="modifyImg!=''">
          <img
            :src="'data:image/png;base64,'+modifyImg"
            width="290px"
            height="147px"
            class="modifyImg"
          />
          <img
            :src="'data:image/png;base64,'+puzzleImg"
            alt
            width="55px"
            height="44px"
            class="puzzleImg"
            :style="{'left':sliderValue+'px'}"
          />
        </div>
        <p class="des">拖动滑块，完成图片拼接，即可完成验证</p>
        <van-slider
          v-model="sliderValue"
          bar-height="20px"
          active-color="#ee0a24"
          class="sliders"
          @change="setLeft()"
          max="290"
          @drag-end="check"
        ></van-slider>
      </div>
    </van-dialog>
  </div>
</template>

<script>
import { Toast } from "vant";
export default {
  name: "pay",
  data() {
    return {
      bannerList: [],
      status: "1",
      account: "",
      show: false,
      showDialog: false,
      confirmAccount: "",
      num: "",
      patternNum: /^[1-9]\d+$/,
      email: "",
      patternEmail: /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
      payType: 1,
      payTypeLIst: [],
      platformId: "",
      htmls: "",
      times: 0,
      left: 0,
      code: "",
      sliderValue: 0,
      imgWidth: 0,
      imgHeight: 0,
      modifyImg: "",
      puzzleWidth: "",
      puzzleHeight: "",
      puzzleImg: "",
      puzzleYAxis: ""
    };
  },
  mounted() {
    this.platformId = this.$route.query.id;
    this.getStatus();
  },
  methods: {
    check() {
      var formData = new FormData();
      formData.append("distance", Number(this.sliderValue));
      this.$api.sliderCheck(formData).then(res => {
        if (res.code == 0) {
          this.showDialog = false;
          this.show = true;

          this.$api
            .checkUserName({
              platformId: this.platformId,
              userName: this.account
            })
            .then(res => {
              if (res.code == 0) {
                if (res.data.status == 1) {
                  this.show = false;
                  return Toast("账号不存在,请核对账号");
                }
                this.code = res.data.code;

                this.createOrder();
              } else {
                this.show = false;
                return Toast(res.msg);
              }
            });
        } else {
          Toast('验证失败，请重试');
          this.getSliderImg();
        }
      });
    },
    beforeClose(action, done) {
      var that = this;
      if (action == "confirm") {
        that.getSliderImg();
        done(false);
      }
    },
    go(item) {
      if (item.jumpUrl) {
        window.location.href = item.jumpUrl;
      }
    },
    setLeft(value) {
      this.left = value;
    },
    createOrder() {
      this.getOrderStatus();
      this.$api
        .placeOrder({
          userName: this.account,
          confirmUserName: this.confirmAccount,
          price: this.num,
          mobile: this.email,
          payType: this.payType,
          platformId: this.platformId,
          invitationCode: this.code
        })
        .then(res => {
          if(res.data.type==1){
             this.htmls = res.data.content;
            this.$nextTick(() => {
              document.getElementsByName("punchout_form")[0].submit();
            });
          }
          if(res.data.type==2){
            window.location.href = res.data.content;
          }
          if(res.data.type==3){
            document.write()
          }
        });
    },
    getOrderStatus() {
      this.timer = setInterval(() => {
        this.times = this.times <= 4 ? this.times + 1 : this.times;
        if (this.times <= 5) {
        }
      }, 5000);
    },
    onFailed(errorInfo) {
      console.log("failed", errorInfo);
    },
    onSubmit() {
      if (this.account !== this.confirmAccount) {
        this.$toast("两次输入账号不一致");
        return false;
      }

      this.getSliderImg();
    },
    getSliderImg() {
      this.$api.getSliderImg({}).then(res => {
        if (res.code == 0) {
          let data = res.data;
          this.sliderValue = 0;
          this.imgWidth = data.imgWidth;
          this.imgHeight = data.imgHeight;
          this.modifyImg = data.modifyImg;
          this.puzzleWidth = data.puzzleWidth;
          this.puzzleHeight = data.puzzleHeight;
          this.puzzleImg = data.puzzleImg;
          this.puzzleYAxis = data.puzzleYAxis;
        }
      });
      this.showDialog = true;
      return false;
    },
    getStatus() {
      this.$api
        .status({
          userId: this.platformId
        })
        .then(res => {
          console.log(res);
          this.status = res.data.status;
          if (res.data.status == "1") {
            this.$router.replace("/404");
          } else {
            this.getBannerList();
            this.getPayTypeList();
          }
        });
    },
    // 获取banner列表
    getBannerList() {
      this.$api.bannerList({ bannerAddress: 1 }).then(res => {
        console.log(res);
        this.bannerList = res.data;
      });
    },
    // 获取支付列表
    getPayTypeList() {
      this.$api.payTypeList({ platformId: this.platformId }).then(res => {
        console.log(res);
        if (res.code == 0) {
          this.payTypeLIst = res.data;
        }
      });
    }
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.loading {
  position: relative;
}
.loading:after {
  content: "";
  width: 100%;
  height: 100vh;
  position: fixed;
  background: #fff;
  z-index: 1200;
  top: 0;
  left: 0;
}
.banner {
  margin-bottom: 8px;
  height: auto;
  min-height: 120px;
}
.banner img {
  width: 100%;
  height: auto;
}
.img {
  display: inline-block;
  vertical-align: middle;
}
.van-button {
  height: 49px;
  border-radius: 10px;
}
.van-button--info {
  background: #f1190c;
  border-color: #f1190c;
}
.wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
}

.block {
  width: 120px;
  height: 120px;
  background-color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
}
.custom-button {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #fff;
}
.contents {
  padding: 0 10px;
}
.imgs {
  width: 290px;
  height: 147px;
  position: relative;
  margin: 5px auto;
}
.modifyImg {
  position: absolute;
  top: 0;
  left: 0;
}
.puzzleImg {
  position: absolute;
  top: 50px;
  left: 0;
}
.des {
  font-size: 12px;
  color: #333;
  text-align: center;
}
.sliders {
  width: 290px;
  margin: 0 auto 5px;
  display: block;
}
</style>
